<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kelurahan extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Kelurahan_model','m');
		$this->load->helper('url');
	  $this->load->library('session');
	}

	public function index()
	{

		//user should login first if willing to see this page
		if(!$this->session->userdata('id_user')) redirect('user/login');

		//if not 9, not an admin :D
		if(8>$this->session->userdata('level')) 
			return redirect('user/home');
	
	
		//load library table agar mudah membuat tabel
		$this->load->library('table');

		$all_kelurahan = $this->m->all();

		$this->table->set_heading('ID', 'Nama', 'ID Kecamatan','ID Polygon');
		foreach($all_kelurahan as $kelurahan){
			$this->table->add_row(
			$kelurahan->id,
			$kelurahan->nama,
			anchor('kecamatan/view/'.$kelurahan->id_kecamatan,
				$kelurahan->id_kecamatan),
			anchor('polygon/view/'.$kelurahan->id_polygon,$kelurahan->id_polygon),
			anchor('kelurahan/view/'.$kelurahan->id,'Lihat'),
			anchor('kelurahan/peta/'.$kelurahan->id,'Peta'),
			anchor('kelurahan/edit/'.$kelurahan->id,'Edit'));
		}
		echo $this->table->generate();
		echo anchor('kelurahan/add','Add');
	}

	/** 
	* Tambah Kelurahan
	* 16 Februari 2013 22:45:33
	*/
	public function add()
	{

		//user should login first if willing to see this page
		if(!$this->session->userdata('id_user')) redirect('user/login');

		//if not 9, not an admin :D
		if(8>$this->session->userdata('level')) 
			return redirect('user/home');

	
		//load helper form untuk memudahkan kita membuat form
		$this->load->helper('form');
		$this->load->library('table');
		
		echo form_open('kelurahan/save');
		$this->table->add_row('Nama Kelurahan',':',form_input('nama'));

		//ambil semua data kecamatan yang ada, dan disimpan ke $options
		$options = $this->m->getAllKecamatan();
		
		//form dropdown untuk membuat combobox atau di HTML namanya select
		$this->table->add_row('Kecamatan',':',form_dropdown('id_kecamatan', $options));
		$this->table->add_row('Polygon',':',form_input('id_polygon'));
		
		echo $this->table->generate();

		//tombol tambah
		echo form_submit('tambah', 'Tambah Kelurahan');
		echo form_close();
	}

	public function save()
	{
		//save our kelurahan
		$data['nama']=$this->input->post('nama');
		$data['id_kecamatan']=$this->input->post('id_kecamatan');
		$data['id_polygon']=$this->input->post('id_polygon');
		
		//$this->load->model('m');//loaded on __construct()

		//simpan id autoincrement yang dihasilkan untuk ditampilkan jika sukses disimpan
		$id_kelurahan = $this->m->insert($data);
		redirect('kelurahan/view/'.$id_kelurahan);

	}

	public function view($id_kelurahan=1)
	{

		//user should login first if willing to see this page
		if(!$this->session->userdata('id_user')) redirect('user/login');

		//if not 9, not an admin :D
		if(8>$this->session->userdata('level')) 
			return redirect('user/home');
	
	
		//$this->load->model('m');//commented since it was autoloaded in __construct()
		//header('Content-Type: text/plain');
		//var_dump( $this->m->get($id_kelurahan));
		$this->load->library('table');
		$kelurahan = $this->m->get($id_kelurahan);
		$this->table->add_row('id',':', $kelurahan->id);
		$this->table->add_row('created_at',':', $kelurahan->created_at);
		$this->table->add_row('updated_at',':', $kelurahan->updated_at);
		$this->table->add_row('Nama Kelurahan',':',$kelurahan->nama);
		$this->table->add_row('ID Kecamatan',':',anchor('kecamatan/view/'.$kelurahan->id_kecamatan,$kelurahan->id_kecamatan));
		$this->table->add_row('Polygon',':',anchor('polygon/view/'.$kelurahan->id_polygon,$kelurahan->id_polygon));


		$this->table->add_row(anchor('kelurahan/add','Add'),anchor('kelurahan/edit/'.$id_kelurahan,'Edit'));
		$this->table->add_row(anchor('kelurahan','Kembali'));
		echo $this->table->generate();
	}


	/**
var_dump($kelurahan);

object(stdClass)#19 (6) {
["id"]=>  string(1) "1"
["created_at"]=>  string(19) "2013-02-16 11:54:13"
["updated_at"]=>  string(19) "2013-02-16 11:54:13"
["nama"]=>  string(12) "Simpang Tiga"
["id_polygon"]=>  string(1) "0"
["id_kecamatan"]=>  string(1) "1"
}
*/
	public function edit($id_kelurahan)
	{

		//user should login first if willing to see this page
		if(!$this->session->userdata('id_user')) redirect('user/login');

		//if not 9, not an admin :D
		if(8>$this->session->userdata('level')) 
			return redirect('user/home');
	
	
		$this->load->library('table');
		$this->load->helper('form');
		$kelurahan = $this->m->get($id_kelurahan);

		echo form_open('kelurahan/update/'.$kelurahan->id);
		$this->table->add_row('id',':', $kelurahan->id);
		$this->table->add_row('created_at',':', $kelurahan->created_at);
		$this->table->add_row('updated_at',':', $kelurahan->updated_at);
		$this->table->add_row('Nama Kelurahan',':',form_input('nama', $kelurahan->nama));
		$options = $this->m->getAllKecamatan();

		$this->table->add_row('Kecamatan',':',form_dropdown('id_kecamatan', $options,$kelurahan->id_kecamatan));
		$this->table->add_row('Polygon',':',form_input('id_polygon',$kelurahan->id_polygon));

		$this->table->add_row(form_submit('Update', 'Update data Kelurahan'));

		echo $this->table->generate();
		echo form_close();


	}

	public function update($id_kelurahan){
		//update our kelurahan
		$data['nama']=$this->input->post('nama');
		$data['id_kecamatan']=(int)$this->input->post('id_kecamatan');
		$data['id_polygon']=(int)$this->input->post('id_polygon');

		$this->m->update($id_kelurahan,$data);
		redirect('kelurahan/view/'.$id_kelurahan);

	}

	public function shape($id){
		header('Content-Type:application/x-javascript; charset=utf-8');

		//get our object
		$o=$this->m->get($id);
		echo 'var kelurahan'.$id."_paths = [\n";
		//try to use preg_split instead of explode ... kadang-kadang error di \r\n
		$ar_shape = preg_split('~\R~',$this->m->getShape($o->id_polygon));
		//print_r($ar_shape);
		//foreach($ar_shape as $a)
		//  echo 'new google.maps.LatLng('.$a."),\n";
		$p=$this->m->getPolygonOptions($id);
		echo "\tnew google.maps.LatLng(";
		echo implode("),\n\tnew google.maps.LatLng(",$ar_shape);
		echo ")\n];";

		echo "\nvar kelurahan".$id."_polyOptions = {";
		echo "\n\tpath: kelurahan".$id."_paths,";
		echo "\n\t strokeColor: \"".$p->strokeColor."\",
		strokeOpacity: ".$p->strokeOpacity.",
		strokeWeight: ".$p->strokeWeight.",
		fillColor: \"".$p->fillColor."\",
		fillOpacity: ".$p->fillOpacity."
	}";

		echo"\n\nkelurahan".$id."_polygon = new google.maps.Polygon(kelurahan".$id."_polyOptions);";

	}

	public function peta($id){
		$o=$this->m->get($id);

		$data['title'] ='Kelurahan '.$o->nama;
		$data['js']= site_url('kelurahan/shape/'.$id);
		$data['js_initialize']='kelurahan'.$id.'_polygon.setMap(map);';
		$data['head']='';
		$this->load->view('kelurahan',$data);
	}
	
	//tampilkan peta semua kelurahan yang telah masuk ke database
	//warning: fungsi ini membuat load server jadi OON ... 
	//banyak yang di close jadinya.
	public function peta_all(){
		//ambil semua kelurahan dari model
		$o=$this->m->all();
		
		//persiapkan array yang akan dikirimkan ke view
		$data['title']='Kelurahan di Kota Pekanbaru';
		$data['js']='';
		$data['head']='';
		$data['js_initialize']='';
		
		foreach($o as $k){
			$data['head'].='<script type="text/javascript" src="'
				.site_url('kelurahan/shape/'.$k->id).'"></script>'."\n";
			$data['js_initialize'].="\nkelurahan".$k->id.'_polygon.setMap(map);';
		}
		$this->load->view('kelurahan',$data);
		
	}



	public function shape_semua(){
		header('Content-Type:application/x-javascript; charset=utf-8');

		//ambil semua kelurahan
		$all=$this->m->all();
		foreach($all as $k){
		
			//get our object
			$o=$this->m->get($k->id);
			echo 'var kelurahan'.$k->id."_paths = [\n";
			//try to use preg_split instead of explode ... kadang-kadang error di \r\n
			$ar_shape = preg_split('~\R~',$this->m->getShape($o->id_polygon));
			//print_r($ar_shape);
			//foreach($ar_shape as $a)
			//  echo 'new google.maps.LatLng('.$a."),\n";
			$p=$this->m->getPolygonOptions($k->id);
			echo "\tnew google.maps.LatLng(";
			echo implode("),\n\tnew google.maps.LatLng(",$ar_shape);
			echo ")\n];";

			echo "\nvar kelurahan".$k->id."_polyOptions = {";
			echo "\n\tpath: kelurahan".$k->id."_paths,";
			echo "\n\t strokeColor: \"".$p->strokeColor."\",
			strokeOpacity: ".$p->strokeOpacity.",
			strokeWeight: ".$p->strokeWeight.",
			fillColor: \"".$p->fillColor."\",
			fillOpacity: ".$p->fillOpacity."
		}";

			echo"\n\nkelurahan".$k->id."_polygon = new google.maps.Polygon(kelurahan"
				.$k->id."_polyOptions);";
		}
		

	}
	

	public function peta_semua(){

		$data['title'] ='Kelurahan di Kota Pekanbaru ';
		$data['js']= site_url('kelurahan/shape_semua/');
		$data['js_initialize']='';
		$data['head']='';
		
		//ambil semua kelurahan
		$all=$this->m->all();
		foreach($all as $k){
			$data['js_initialize'].="\nkelurahan".$k->id.'_polygon.setMap(map);';		
		}
		
		$this->load->view('kelurahan',$data);
	}	
	
	/*
	public function testPoligonOptions($id){
		var_dump($this->m->getPolygonOptions($id));
	}
	*/
}
